{% extends 'base.html' %}
{% load static %}

{% block title %}{{ title }} - AI故事生成系统{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h1 class="h3 mb-0">故事排行榜</h1>
                </div>
                <div class="card-body">
                    <!-- 排行榜类型切换 -->
                    <div class="mb-4">
                        <div class="d-flex gap-2 mb-3">
                            <span class="fw-bold">排行类型：</span>
                            <a href="?type=likes&time={{ time_range }}{% if selected_category %}&category={{ selected_category }}{% endif %}" class="badge rounded-pill {% if ranking_type == 'likes' %}bg-primary{% else %}bg-secondary{% endif %} text-decoration-none">点赞榜</a>
                            <a href="?type=views&time={{ time_range }}{% if selected_category %}&category={{ selected_category }}{% endif %}" class="badge rounded-pill {% if ranking_type == 'views' %}bg-primary{% else %}bg-secondary{% endif %} text-decoration-none">阅读榜</a>
                            <a href="?type=comments&time={{ time_range }}{% if selected_category %}&category={{ selected_category }}{% endif %}" class="badge rounded-pill {% if ranking_type == 'comments' %}bg-primary{% else %}bg-secondary{% endif %} text-decoration-none">评论榜</a>
                        </div>
                        <div class="d-flex gap-2 mb-3">
                            <span class="fw-bold">时间范围：</span>
                            <a href="?type={{ ranking_type }}&time=day{% if selected_category %}&category={{ selected_category }}{% endif %}" class="badge rounded-pill {% if time_range == 'day' %}bg-success{% else %}bg-secondary{% endif %} text-decoration-none">日榜</a>
                            <a href="?type={{ ranking_type }}&time=week{% if selected_category %}&category={{ selected_category }}{% endif %}" class="badge rounded-pill {% if time_range == 'week' %}bg-success{% else %}bg-secondary{% endif %} text-decoration-none">周榜</a>
                            <a href="?type={{ ranking_type }}&time=month{% if selected_category %}&category={{ selected_category }}{% endif %}" class="badge rounded-pill {% if time_range == 'month' %}bg-success{% else %}bg-secondary{% endif %} text-decoration-none">月榜</a>
                            <a href="?type={{ ranking_type }}&time=all{% if selected_category %}&category={{ selected_category }}{% endif %}" class="badge rounded-pill {% if time_range == 'all' %}bg-success{% else %}bg-secondary{% endif %} text-decoration-none">总榜</a>
                        </div>
                        <div class="d-flex gap-2 flex-wrap">
                            <span class="fw-bold">故事分类：</span>
                            <a href="?type={{ ranking_type }}&time={{ time_range }}" class="badge rounded-pill {% if not selected_category %}bg-info{% else %}bg-secondary{% endif %} text-decoration-none">全部</a>
                            {% for category in categories %}
                            <a href="?type={{ ranking_type }}&time={{ time_range }}&category={{ category.id }}" class="badge rounded-pill {% if selected_category == category.id|stringformat:'i' %}bg-info{% else %}bg-secondary{% endif %} text-decoration-none">{{ category.name }}</a>
                            {% endfor %}
                        </div>
                    </div>
                    
                    <!-- 排行榜列表 -->
                    <div class="list-group">
                        {% for story in stories %}
                        <a href="{% url 'story:detail' story.pk %}" class="list-group-item list-group-item-action">
                            <div class="d-flex align-items-center">
                                <div class="rank-number me-3 {% if story.rank <= 3 %}text-danger{% endif %} fw-bold" style="font-size: 1.2rem; min-width: 2rem;">
                                    {% if story.rank <= 3 %}
                                    <i class="bi bi-trophy-fill"></i>
                                    {% endif %}
                                    {{ story.rank }}
                                </div>
                                <div class="flex-grow-1">
                                    <h5 class="mb-1">{{ story.title }}</h5>
                                    <p class="mb-1 text-muted small">
                                        <span class="badge bg-secondary">{{ story.get_category_display }}</span>
                                        <i class="bi bi-person ms-2 me-1"></i>{{ story.author.username }}
                                        <i class="bi bi-calendar-date ms-2 me-1"></i>{{ story.created_at|date:"Y-m-d" }}
                                    </p>
                                    <p class="mb-0 text-muted">{{ story.content|striptags|truncatechars:100 }}</p>
                                </div>
                                <div class="ms-3 text-end" style="min-width: 6rem;">
                                    <div>
                                        <i class="bi bi-eye"></i> {{ story.views }}
                                    </div>
                                    <div>
                                        <i class="bi bi-heart"></i> {{ story.likes }}
                                    </div>
                                    <div>
                                        <i class="bi bi-chat"></i> 
                                        {% if ranking_type == 'comments' %}
                                            {{ story.comments_count }}
                                        {% else %}
                                            {{ story.comments.count }}
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </a>
                        {% empty %}
                        <div class="text-center p-5">
                            <p class="text-muted">暂无数据</p>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 